// 引入axios
import axios from 'axios';
import router from '@/router'

let baseUrl = "http://localhost:8000/";

// 创建axios实例
const httpService = axios.create({
    // url前缀-'http:xxx.xxx'
    // baseURL: process.env.BASE_API, // 需自定义
    baseURL: baseUrl,
    // 请求超时时间
    timeout: 3000 // 需自定义
});

//添加请求和响应拦截器
// 添加请求拦截器
httpService.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    config.headers.AUTHORIZATION = window.sessionStorage.getItem('token');
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

import { ElMessage, ElMessageBox } from 'element-plus'

// 添加响应拦截器
httpService.interceptors.response.use(function (response) {
    // console.log('拦截 response:',response)
    // 对响应数据做点什么
    if (response.data.code === 401 || response.data.code === 403) {
        // 清除本地token
        window.sessionStorage.removeItem('token');
        // 弹出提示，3秒后跳转
        ElMessageBox.alert(response.data.msg, '提示');
        // if (window.ElMessage) {
        //     window.ElMessage.error(response.data.msg);
        // } else {
        //     alert(response.data.msg);
        // }
        setTimeout(function() {
            router.push('/login')
        }, 3000);
    }
    
    return response;
}, function (error) {
    // 对响应错误做全局处理
    // if (error.response.data.code === 401 || error.response.data.code === 403) {
    //     // 清除本地token
    //     window.sessionStorage.removeItem('token');
    //     // 弹出提示，3秒后跳转
    //     if (window.ElMessage) {
    //         // window.ElMessage.error(response.data.msg);
    //     } else {
    //         // alert(response.data.msg);
    //     }
    //     setTimeout(function() {
    //         window.location.href = 'http://10.8.77.78:8080/#/login';
    //     }, 30);
    // }
    if (error.response && error.response.status) {
        switch (error.response.status) {
            case 400:
                ElMessage.error('请求参数错误!');
                break;
            case 401:
                ElMessage.error('未授权，请登录!');
                break;
            case 403:
                ElMessage.error('没有权限访问!');
                break;
            case 404:
                ElMessage.error('请求地址不存在!');
                break;
            case 500:
                ElMessage.error('服务器内部错误!');
                break;
            default:
                ElMessage.error('请求失败!');
        }
    }
    return Promise.reject(error);
});

/*网络请求部分*/

/*
 *  get请求
 *  url:请求地址
 *  params:参数
 * */
export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'get',
            params: params
        }).then(response => {
            resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}

/*
 *  post请求
 *  url:请求地址
 *  params:参数
 * */
export function post(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'post',
            data: params
        }).then(response => {
            resolve(response);
        }).catch(error => {
            console.log(error)
            reject(error);
        });
    });
}

/*
 *  delete请求
 *  url:请求地址
 *  params:参数
 * */
export function del(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'delete',
            data: params
        }).then(response => {
            console.log(response)
            resolve(response);
        }).catch(error => {
            console.log(error)
            reject(error);
        });
    });
}


/*
 *  文件上传
 *  url:请求地址
 *  params:参数
 * */
export function fileUpload(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'post',
            data: params,
            headers: { 'Content-Type': 'multipart/form-data' }
        }).then(response => {
            resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}

export function getServerUrl() {
    return baseUrl;
}

export default {
    get,
    post,
    del,
    fileUpload,
    getServerUrl
}
